/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiummath;

import java.math.MathContext;
import java.math.RoundingMode;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides conversion functions for <tt>CadmiumMath</tt> concrete types.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
final class Misc {

    /**
     * No instance of this class.
     */
    private Misc() {
    } // end empty constructor

    /**
     * Converts a rounding mode from Java into OCaml representation.
     * @param rm rounding mode, in Java representation
     * @return the passed rounding mode, in OCaml representation
     */
    static Value encodeRoundingMode(final RoundingMode rm) {
        switch (rm) {
        case CEILING: return Value.ZERO;
        case DOWN: return Value.ONE;
        case FLOOR: return Value.TWO;
        case HALF_DOWN: return Value.createFromLong(3);
        case HALF_EVEN: return Value.createFromLong(4);
        case HALF_UP: return Value.createFromLong(5);
        case UNNECESSARY: return Value.createFromLong(6);
        case UP: return Value.createFromLong(7);
        default:
            // never reached
            assert false : "invalid case";
            return Value.ZERO;
        } // end switch
    } // end method 'encodeRoundingMode(RoundingMode)'

    /**
     * Converts a rounding mode from OCaml into Java representation.
     * @param rm rounding mode, in OCaml representation
     * @return the passed rounding mode, in Java representation
     */
    static RoundingMode decodeRoundingMode(final Value rm) {
        switch (rm.asLong()) {
        case 0: return RoundingMode.CEILING;
        case 1: return RoundingMode.DOWN;
        case 2: return RoundingMode.FLOOR;
        case 3: return RoundingMode.HALF_DOWN;
        case 4: return RoundingMode.HALF_EVEN;
        case 5: return RoundingMode.HALF_UP;
        case 6: return RoundingMode.UNNECESSARY;
        case 7: return RoundingMode.UP;
        default:
            // never reached
            assert false : "invalid case";
            return null;
        } // end switch
    } // end method 'decodeRoundingMode(Value)'

    /**
     * Converts a math context from Java into OCaml representation.
     * @param mc math context, in Java representation
     * @return the passed math context, in OCaml representation
     */
    static Value encodeMathContext(final MathContext mc) {
        if (mc == MathContext.DECIMAL32) {
            return Value.ZERO;
        } else if (mc == MathContext.DECIMAL64) {
            return Value.ONE;
        } else if (mc == MathContext.DECIMAL128) {
            return Value.TWO;
        } else if (mc == MathContext.UNLIMITED) {
            return Value.createFromLong(3);
        } else {
            final Block res = Block.createBlock(0,
                                                Value.createFromLong(mc.getPrecision()),
                                                encodeRoundingMode(mc.getRoundingMode()));
            return Value.createFromBlock(res);
        } // end if/elsif/else
    } // end method 'encodeMathContext(MathContext)'

    /**
     * Converts a math context from OCaml into Java representation.
     * @param mc math context, in OCaml representation
     * @return the passed math context, in Java representation
     */
    static MathContext decodeMathContext(final Value mc) {
        if (mc.isLong()) {
            switch (mc.asLong()) {
            case 0: return MathContext.DECIMAL32;
            case 1: return MathContext.DECIMAL64;
            case 2: return MathContext.DECIMAL128;
            case 3: return MathContext.UNLIMITED;
            default:
                // never reached
                assert false : "invalid case";
                return null;
            } // end switch
        } else {
            final Block b = mc.asBlock();
            return new MathContext(b.get(0).asLong(), decodeRoundingMode(b.get(1)));
        } // end if/else
    } // end method 'decodeMathContext(Value)'

} // end class 'Misc'
